class Solution {
    public int findTheCity(int n, int[][] edges, int distanceThreshold) {
        // 构建邻接矩阵
        int[][] graph = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                graph[i][j] = (int)1e9;
            }
            graph[i][i] = 0;
        }
        for (int[] edge : edges) {
            graph[edge[0]][edge[1]] = edge[2];
            graph[edge[1]][edge[0]] = edge[2];
        }
        // Floyd 算法
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
                }
            }
        }
        // 根据题意统计结果
        int lastCount = (int)1e9;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = 0; j < n; j++) {
                if (i != j && graph[i][j] <= distanceThreshold) {
                    count++;
                }
            }
            if (count <= lastCount) {
                lastCount = count;
                ans = i;
            }
        }
        return ans;
    }
}
